// --------------------------------------------------------------------------------------------------------------------
// <copyright file="IAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Runtime
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// Describes the lifetime management contract that every agent must implement.
    /// </summary>
    public interface IAgent
    {
        /// <summary>
        /// Gets a friendly name, unique within the agent collection managed by the same locator.
        /// </summary>
        string Name { get; }

        /// <summary>
        /// Called to allow the agent to wire itself to other agents via the provided locator. 
        /// The agent can use the locator to discover other agents by name and contract.
        /// </summary>
        /// <param name="locator">A locator that provides the means to discover other agents.</param>
        void Initialize(AgentLocator locator);

        /// <summary>
        /// Called after Initialize, when the agent is first activated by a subscriber.
        /// The agent will usually activate the upstream agents it subscribes to.
        /// </summary>
        void OnActivated();

        /// <summary>
        /// Called before deactivating the agent, to allow it to deactivate its partners.
        /// </summary>
        void OnDeactivated();
    }

    /// <summary>
    /// Allows an agent to receive subscription messages.
    /// An agent may implement any number of ISubscriptionReceiver interfaces, as long as they have different type parameters.
    /// </summary>
    /// <typeparam name="TMessage">A type that uniquely describes the subscription message signature.</typeparam>
    /// <remarks>
    /// The interface enforces async, loose coupling between agents, by providing a message-based contract instead of an imperative one (i.e. methods exposed directly to the partner agent).
    /// This allows the framework to intercept the messages and provide additional services, 
    /// e.g. versioning and remote execution (agents run potentially on different machines, without the need to publish additional service entry points).
    /// </remarks>
    public interface ISubscriptionReceiver<TMessage>
        where TMessage : AgentMessage
    {
        /// <summary>
        /// Receives a message of the specified type. 
        /// </summary>
        /// <param name="message">The message to process.</param>
        void Receive(TMessage message);
    }

    /// <summary>
    /// Allows an agent to receive subscription messages of types not known at compile time.
    /// </summary>
    /// <remarks>
    /// The interface enforces async, loose coupling between agents, by providing a message-based contract instead of an imperative one (i.e. methods exposed directly to the partner agent).
    /// This allows the framework to intercept the messages and provide additional services, 
    /// e.g. versioning and remote execution (agents run potentially on different machines, without the need to publish additional service entry points).
    /// </remarks>
    public interface ISubscriptionReceiver
    {
        /// <summary>
        /// Gets the set of types the agent is willing to accept.
        /// </summary>
        IEnumerable<Type> SubscriptionTypes { get; }

        /// <summary>
        /// Receives a message of any type.
        /// </summary>
        /// <param name="message">The message to process.</param>
        void Receive(AgentMessage message);
    }

    /// <summary>
    /// Convenience interface used when wiring agents. 
    /// When implementing an agent, use IControlReceiver instead.
    /// </summary>
    /// <typeparam name="TMessage">The type of control messages accepted by the agent.</typeparam>
    public interface IControllable<TMessage>
        where TMessage : AgentMessage
    {
        /// <summary>
        /// Gets a friendly name, unique within the agent collection managed by the same locator.
        /// </summary>
        string Name { get; }
    }

    /// <summary>
    /// Allows an agent to receive control messages. 
    /// The control message paradigm doesn't support return values, as sending a control message is a fire-and-forget operation. 
    /// However, the agent sending the control message can subscribe to the agent receiving the control message to observe the results of the operation.
    /// An agent may implement any number of IControlReceiver interfaces, as long as they have different type parameters.
    /// </summary>
    /// <typeparam name="TMessage">A type that uniquely describes the command signature.</typeparam>
    /// <remarks>
    /// The interface enforces async, loose coupling between agents, by providing a message-based contract instead of an imperative one (i.e. methods exposed directly to the partner agent).
    /// This allows the framework to intercept the messages and provide additional services, 
    /// e.g. versioning and remote execution (agents run potentially on different machines, without the need to publish additional service entry points).
    /// </remarks>
    public interface IControlReceiver<TMessage> : IControllable<TMessage>
        where TMessage : AgentMessage
    {
        /// <summary>
        /// Receives a message of the specified type. 
        /// </summary>
        /// <param name="message">The message to process.</param>
        void ReceiveControl(TMessage message);
    }

    /// <summary>
    /// Allows an agent to receive control messages of types not known at compile time.. 
    /// The control message paradigm doesn't support return values, as sending a control message is a fire-and-forget operation. 
    /// However, the agent sending the control message can subscribe to the agent receiving the control message to observe the results of the operation.
    /// An agent may implement any number of IControlReceiver interfaces, as long as they have different type parameters.
    /// </summary>
    /// <remarks>
    /// The interface enforces async, loose coupling between agents, by providing a message-based contract instead of an imperative one (i.e. methods exposed directly to the partner agent).
    /// This allows the framework to intercept the messages and provide additional services, 
    /// e.g. versioning and remote execution (agents run potentially on different machines, without the need to publish additional service entry points).
    /// </remarks>
    public interface IControlReceiver
    {
        /// <summary>
        /// Gets the set of types the agent is willing to accept.
        /// </summary>
        IEnumerable<Type> ControlTypes { get; }

        /// <summary>
        /// Receives a control message of any type. 
        /// </summary>
        /// <param name="message">The message to process.</param>
        void ReceiveControl(AgentMessage message);

        /// <summary>
        /// Returns an IControllable if the agent accepts the provided type as control message.
        /// Useful in maintaining type safety when creating a manifest.
        /// </summary>
        /// <typeparam name="T">The type of message</typeparam>
        /// <returns>An IControllable for the specified message type.</returns>
        IControllable<T> AsControllable<T>() where T : AgentMessage;
    }

    /// <summary>
    /// Convenience interface used when wiring agents. When implementing an agent, use IMessagePublisher instead.
    /// </summary>
    /// <typeparam name="TMessage">The type of messages published by the agent.</typeparam>
    public interface IProducer<TMessage>
        where TMessage : AgentMessage
    {
        /// <summary>
        /// Gets a friendly name, unique within the agent collection managed by the same locator.
        /// </summary>
        string Name { get; }
    }

    /// <summary>
    /// Allows an agent to publish messages.
    /// </summary>
    /// <typeparam name="TMessage">The type of messages published by the agent.</typeparam>
    /// <remarks>The interface exists solely to make code discoverability easier.</remarks>
    public interface IMessagePublisher<TMessage> : IProducer<TMessage>
        where TMessage : AgentMessage
    {
        /// <summary>
        /// Called to give the implementing agent a forwarder that it can use to publish messages.
        /// </summary>
        /// <param name="forwarder">A forwarder that can be used to publish messages of the right type.</param>
        void InitializePublisher(IForwarder<TMessage> forwarder);
    }

    /// <summary>
    /// Allows an agent to publish messages of types not known at compile time.
    /// </summary>
    /// <remarks>The interface exists solely to make code discoverability easier.</remarks>
    public interface IMessagePublisher
    {
        /// <summary>
        /// Gets the set of types the agent will be publishing.
        /// </summary>
        IEnumerable<Type> PublishedTypes { get; }

        /// <summary>
        /// Called to give the implementing agent a forwarder that it can use to publish messages.
        /// </summary>
        /// <param name="forwarders">The set of forwarders that can be used to publish messages of the right type.</param>
        void InitializePublishers(Dictionary<Type, IForwarder> forwarders);

        /// <summary>
        /// Returns an IProducer if the agent publishes the provided type.
        /// Useful in maintaining type safety when creating a manifest.
        /// </summary>
        /// <typeparam name="T">The type of message</typeparam>
        /// <returns>An IProducer for the specified message type.</returns>
        IProducer<T> AsProducer<T>() where T : AgentMessage;
    }
}
